package com.example.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.Result;
import com.example.entity.Employee;
import com.example.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    //新增数据
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return  Result.success();
    }

    //更新数据
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return  Result.success();
    }

    //删除单个数据
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        employeeService.deleteById(id);
        return  Result.success();
    }

    //批量删除数据
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) //数组用@RequestBod接收
    {
        employeeService.deleteBatch(ids);
        return  Result.success();
    }

    //根据id查询
    @GetMapping("/selectById/{id}")   // http://localhost:9090/employee/selectById/1/101
    public Result selectById(@PathVariable Integer id) {
        Employee employee = employeeService.selectById(id);
        return  Result.success(employee);
    }

//查询所有的数据
    @GetMapping("/selectAll")   // /employee/selectAll
    public Result selectAll(Employee employee) {
        List<Employee> list = employeeService.selectAll(employee);
        return  Result.success(list);
    }

    //分页查询数据
    /*
    pageNum:当前页码；pageSize：每页多少个
     */
    @GetMapping("/selectPage")   // /employee/selectAll
    public Result selectPage(Employee employee,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Employee> pageInfo = employeeService.selectPage(employee, pageNum, pageSize);
        return  Result.success(pageInfo);
    }

    /*
    导出excel
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        //1.拿到所有员工数据
        List<Employee> employeeList = employeeService.selectAll(null);
        //2.构建excel writer
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //设置中文表头
        writer.addHeaderAlias("username", "账号");
        writer.addHeaderAlias("name", "名称");
        writer.addHeaderAlias("sex", "性别");
        writer.addHeaderAlias("description", "介绍");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true); //写了之后只会导出加了别名的字段
        //4.写出数据到writer;
        writer.write(employeeList,true);
        //设置输出的文件名,以及输出流的头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("员工信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        //6.写出到输出流,并关闭
        ServletOutputStream os = response.getOutputStream();
        writer.flush(os);
        writer.close();
    }

    /**
     * excel 导入
     */
    @PostMapping("/import")
    public Result importData(MultipartFile file) throws Exception {
        //1.拿到输入流，构建reader
        InputStream inputStream = file.getInputStream();
        ExcelReader reader =  ExcelUtil.getReader(inputStream);
        //2.读取excel中的数据
        reader.addHeaderAlias("账号","username");
        reader.addHeaderAlias("名称","name");
        reader.addHeaderAlias("性别","sex");
        reader.addHeaderAlias("介绍","description");
        List<Employee> employeeList = reader.readAll(Employee.class);
        //3.写入list数据到数据库
        for (Employee employee : employeeList) {
            employeeService.add(employee);
        }
        return  Result.success();
    }



//    @GetMapping("/selectOne")   // http://localhost:9090/employee/selectOne?id=4&no=104
//    public Result selectOne(@RequestParam Integer id, @RequestParam (required = false) String no) {
//        Employee employee = employeeService.selectById(id);
//        return  Result.success(employee);
//    }
//
//    @GetMapping("/selectList")   // http://localhost:9090/employee/selectOne?id=4&no=104
//    public Result selectList(Employee employee) {
//        List<Employee> List = employeeService.selectList(employee);
//        return  Result.success(List);
//    }
}
